<?php

namespace app\utils\aliyun;

use app\utils\http\HttpClient;
use GuzzleHttp\Exception\GuzzleException;
use think\Exception;

class VerifyIdName
{
    protected string $host = "https://dfidveri.market.alicloudapi.com";
    protected string $path = "/verify_id_name";
    protected string $method = "POST";
    protected string $appcode = "a68b0ef8c8be43aeb5c73e906006e510";
    protected string $url;
    protected array $headers = array();
    
    public function __construct()
    {
        $this->url = $this->host . $this->path;
        $this->headers = [
            "Authorization" => 'APPCODE ' . $this->appcode,
            //根据API的要求，定义相对应的Content-Type
            "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"
        ];
    }
    
    public static function instance(): VerifyIdName
    {
        return new static();
    }
    
    /**
     * @param $id_number
     * @param $name
     * @return object|string
     * @throws GuzzleException
     * @throws Exception
     */
    public function verify($id_number, $name)
    {
        // $bodys = "id_number=445122********33&name=%E9%BB%84%E5%A4%A7%E5%A4%A7";
        $response = HttpClient::createInstance()->postRequest($this->url, [
            'id_number' => $id_number,
            'name' => $name
        ], $this->headers);
        Logs($response, 'VerifyIdName');
        
        if (!isset($response->state) || $response->state != 1) {
            return false;
            // return $response;
        }
        return true;
        // return $response;
        // 成功
        //{
        // 	"request_id": "TIDc88d5d4755c643b98ffdde9aa7156b25",
        // 	"status": "OK",
        // 	"state": 1,
        // 	"result_code": 1,
        // 	"result_message": "一致"
        // }
        
        // 失败
        //{
        // 	"status": "OK",
        // 	"state": 2,
        // 	"result_code": 0,
        // 	"result_message": "不一致",
        // 	"request_id": "TID914b13becea8448d905371d60bb2aa92"
        // }
        
        // return $response;
    }

}